const { getBanner, getGoodsList } = require("../../api/index.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    swiperOptions: {
      indicatorDots: true,
      autoplay: true,
      interval: 3000,
      duration: 1000,
      swiperDatas: []
    },
    navDatas: [
      {
        icon: "photo-fail",
        text: "数码",
        color: "#ff0000"
      },
      {
        icon: "like",
        text: "生鲜",
        color: "#ff0000"
      },
      {
        icon: "gem",
        text: "会员",
        color: "#ff0000"
      },
      {
        icon: "gift",
        text: "优惠",
        color: "#ff0000"
      },
      {
        icon: "bill",
        text: "充值",
        color: "#ff0000"
      },
      {
        icon: "hot",
        text: "外卖",
        color: "#ff0000"
      },
      {
        icon: "gift-card",
        text: "领券",
        color: "#ff0000"
      },
      {
        icon: "shop-collect",
        text: "美食",
        color: "#ff0000"
      }
    ],
    page: 1,
    goodslist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getBanner().then((res) => {
      this.setData({
        swiperOptions: {
          indicatorDots: true,
          autoplay: true,
          interval: 3000,
          duration: 1000,
          swiperDatas: res.data.data.result
        }
      })
    })
    this.http(this.data.page)
  },
  http(page) {
    getGoodsList({ page: page }).then((res) => {
      if(!res.data.msg){
        this.setData({
          goodslist: this.data.goodslist.concat(res.data.data.result)
        })
      }else {
        wx.showToast({
          title: res.data.msg,
          icon: "error",
          duration: 2000
        })
      }
    })
  },
  //点击搜索框
  clickSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page += 1
    })
    this.http(this.data.page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})